package com.yswee.framework.widget.switchtab;

import java.util.ArrayList;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

import androidx.viewpager.widget.PagerAdapter;
import androidx.viewpager.widget.ViewPager;

public class SwitchTabView extends ViewPager implements ISwitchTabView {

	protected Context mContext;
	protected int curTabIndex;
	protected boolean mInitialized;
	protected ISwitchTabViewAction mSwitchTabViewAction;
	protected ArrayList<OnPageChangeListener> mOnPageChangeListener;
	
	protected ISwitchTabBar tabBar;
	protected View[] viewList;
	
	public void setSwitchTabViewAction(ISwitchTabViewAction l) {
		mSwitchTabViewAction = l;
	}
	
	@Override
	public void setOnPageChangeListener(OnPageChangeListener listener) {
		if (mOnPageChangeListener == null) {
			mOnPageChangeListener = new ArrayList<ViewPager.OnPageChangeListener>();
		}
		mOnPageChangeListener.remove(listener);
		mOnPageChangeListener.add(listener);
	}
	
	/**
	 * 初始化的index
	 * @return
	 */
	protected int getInitializeTabIndex() {
		if (mSwitchTabViewAction != null) {
			return mSwitchTabViewAction.getInitializeTabIndex();
		} else {
			return 0;
		}
	}
	
	/**
	 * 获取当前显示的view的index
	 * @return
	 */
	public int getCurrentTabIndex() {
		return curTabIndex;
	}

	/**
	 * 获取当前显示的view
	 * @return
	 */
	public View getCurrentView() {
		return viewList[curTabIndex % viewList.length];
	}
	
	protected PagerAdapter mPagerAdapter = new PagerAdapter() {
	    @Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
		
		@Override
		public int getCount() {
			return tabBar != null ? tabBar.getTabCount() : 0;
		}

		@Override
		public Object instantiateItem(View container, int position) {
			View view = viewList[position % viewList.length];
			if (view.getParent() == null) {
				((ViewPager) container).addView(view);
			}
			if (mSwitchTabViewAction != null) {
				mSwitchTabViewAction.instantiateItem(position, view);
			}
			view.setTag(position);
			return view;
		}
		
		@Override
		public void destroyItem(View container, int position, Object object) {
			/*
			View view = viewList[position % viewList.length];
			try {
				((ViewPager) container).removeView(view);
			} catch(Exception e) {
				e.printStackTrace();
			}
			*/
		}
	};
	
	protected OnPageChangeListener mOnPageChangeListenerProxy = new OnPageChangeListener() {
        @Override
        public void onPageSelected(int position) {
            onTabChanged(position, false);
            if (mOnPageChangeListener != null) {
            	for (int i = 0; i < mOnPageChangeListener.size(); i++) {
            		mOnPageChangeListener.get(i).onPageSelected(position);
            	}
			}
        }

        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            if (mOnPageChangeListener != null) {
            	for (int i = 0; i < mOnPageChangeListener.size(); i++) {
            		mOnPageChangeListener.get(i).onPageScrolled(position, positionOffset, positionOffsetPixels);
            	}
			}
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            if (mOnPageChangeListener != null) {
            	for (int i = 0; i < mOnPageChangeListener.size(); i++) {
            		mOnPageChangeListener.get(i).onPageScrollStateChanged(state);
            	}
			}
        }
    };

	public SwitchTabView(Context context) {
		super(context);
		initializeLayout(context);
	}
	
	public SwitchTabView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeLayout(context);
	}
	
	protected void initializeLayout(Context context) {
		mContext = context;
	}
	
	@Override
	public void initializeView() {
		if (mInitialized) {
			mPagerAdapter.notifyDataSetChanged();
			return;
		}
		mInitialized = true;
		if (isInEditMode()) {
			return;
		}
		initializeIntent();
		initializeTabBar();
		initializeViewPager();
	}

	protected void initializeIntent() {
		if(mSwitchTabViewAction != null) {
			viewList = mSwitchTabViewAction.getTabView();
		} else {
			throw new IllegalStateException("please call setSwitchTabAction() method");
		}
	}
	
	protected void initializeTabBar() {
		if(mSwitchTabViewAction != null) {
			tabBar = mSwitchTabViewAction.getTabBar();
//			if(tabBar.getTabCount() < viewList.length) {
//				throw new IllegalStateException("tabbar's tabcount can't less than tabview's tabcount");
//			}
		} else {
			throw new IllegalStateException("please call setSwitchTabAction() method");
		}
		
	}
	
	@SuppressWarnings("deprecation")
	protected void initializeViewPager() {
		curTabIndex = -1;
		super.setOnPageChangeListener(mOnPageChangeListenerProxy);
		setAdapter(mPagerAdapter);
		onTabChanged(getInitializeTabIndex(), true);
	}

	@Override
	public void onTabChanged(int index, boolean applyViewPager) {
		if(curTabIndex != index) {
			curTabIndex = index;
			if (applyViewPager) {
				setCurrentItem(index);
			}
		}
		if(tabBar != null) {
			tabBar.setTabIndex(index);
		}
	}

	public interface ISwitchTabViewAction {
		ISwitchTabBar getTabBar();
		View[] getTabView();
		void instantiateItem(int index, View view);
		int getInitializeTabIndex();
	}
	
}